package com.services.weather;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;
import android.util.Xml;

public class YahooForecastParser {

	private static final String ns = null;
	
	private void inputToString(InputStream in) throws IOException {
		InputStreamReader is = new InputStreamReader(in);
		StringBuilder sb=new StringBuilder();
		BufferedReader br = new BufferedReader(is);
		String read = br.readLine();

		while(read != null) {
		    //System.out.println(read);
		    sb.append(read);
		    read =br.readLine();

		}

		System.out.println("Forecast xml: " + sb.toString());
	}
	
    public List parse(InputStream in) throws XmlPullParserException, IOException {
        try {
        	//inputToString(in);
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);
            parser.nextTag();
            return readData(parser);
        } finally {
            in.close();
        }
    }

	
	private List readData(XmlPullParser parser) throws XmlPullParserException, IOException {
		List entries = new ArrayList();

	    parser.require(XmlPullParser.START_TAG, ns, "rss");
	    while (parser.next() != XmlPullParser.END_TAG) {
	        if (parser.getEventType() != XmlPullParser.START_TAG) {
	            continue;
	        }
	        String name = parser.getName();
	        // Starts by looking for the entry tag
	        if (name.equals("channel")) {
	            entries = readChannel(parser);
	        }
	    }
	    
	    return entries;
	}
	
	private List readChannel(XmlPullParser parser) throws XmlPullParserException, IOException {
		List entries = new ArrayList();

	    parser.require(XmlPullParser.START_TAG, ns, "channel");
	    while (parser.next() != XmlPullParser.END_TAG) {
	        if (parser.getEventType() != XmlPullParser.START_TAG) {
	            continue;
	        }
	        String name = parser.getName();
	        // Starts by looking for the entry tag
	        if (name.equals("item")) {
	            entries.add(Item.readItem(parser));
	        }
	        else
	        	skip(parser);
	    }
	    
	    return entries;
	}
	
	private static void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
	    if (parser.getEventType() != XmlPullParser.START_TAG) {
	        throw new IllegalStateException();
	    }
	    int depth = 1;
	    while (depth != 0) {
	        switch (parser.next()) {
	        case XmlPullParser.END_TAG:
	            depth--;
	            break;
	        case XmlPullParser.START_TAG:
	            depth++;
	            break;
	        }
	    }
	}
	
	public static class Item {
		
		public final String code;
		public final String description;
		public Item(String code, String description) {
			this.code = code;
			this.description = description;
		}
		
		public static Item readItem(XmlPullParser parser) throws XmlPullParserException, IOException {
		    parser.require(XmlPullParser.START_TAG, ns, "item");
		    String code = null;
		    String description = null;
		    while (parser.next() != XmlPullParser.END_TAG) {
		        if (parser.getEventType() != XmlPullParser.START_TAG) {
		            continue;
		        }
		        String name = parser.getName();
		        if (name.equals("yweather:condition")) {
		            code = readCode(parser);
		        } else if(name.equals("description")) {
		        	description = readText(parser);
		        	Log.d("Parser", description);
		        }
		        else {
		            skip(parser);
		        }
		    }
		    return new Item(code, description);
		}
		
		// Processes woeid tags in the feed.
		private static String readCode(XmlPullParser parser) throws IOException, XmlPullParserException {
		    parser.require(XmlPullParser.START_TAG, ns, "yweather:condition");
		    
		    String code = parser.getAttributeValue(null, "code");  
		    
		    parser.nextTag();
		    parser.require(XmlPullParser.END_TAG, ns, "yweather:condition");
		    return code;
		}
		
		// Extract text values.
		private static String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
		    String result = "";
		    if (parser.next() == XmlPullParser.TEXT) {
		        result = parser.getText();
		        parser.nextTag();
		    }
		    return result;
		}
		
		// Skip unimportant tags
		private static void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
		    if (parser.getEventType() != XmlPullParser.START_TAG) {
		        throw new IllegalStateException();
		    }
		    int depth = 1;
		    while (depth != 0) {
		        switch (parser.next()) {
		        case XmlPullParser.END_TAG:
		            depth--;
		            break;
		        case XmlPullParser.START_TAG:
		            depth++;
		            break;
		        }
		    }
		}
	}
}
